tool
extends EditorScript

func _run():
	
	var code : String = """
name: String,
age:= 0,
pos : Vector2 = Vector2(0,0),
speed: float = 10.0,
tex : = "ehllo",
o = Transform2D()
"""
	var data = parse_parameter(code)
	print(JSON.print(data, "\t"))


var parameter_regex = RegEx.new()
var parameter_pattern = ""

func parse_parameter(code: String):
	# 参数名
	parameter_pattern += "(?<ArgName>\\w+)"
	parameter_pattern += "("
	# 参数冒号
	parameter_pattern += "\\s*(:)?\\s*"
	# 参数类型
	parameter_pattern += "(?<ArgType>\\w+)?"
	# 等号
	parameter_pattern += "\\s*(=?)\\s*"
	
	parameter_pattern += ")"
	
	# 匹配 Value
	parameter_pattern += "(?<Value>"
	# 匹配 Vector | Transform | Rect2 等类型的值
	parameter_pattern += "(Vector[2|3]|Transform(2D)?|Rect2)(\\([^\\)]*\\))"
	parameter_pattern += "|([^,]*)"
	parameter_pattern += ")?"
	
	parameter_pattern += "\\s*,?\\s*"	# 逗号结尾
	
	parameter_regex.compile(parameter_pattern)
	var results = parameter_regex.search_all(code)
	var list : Array = []
	for result in results:
		result = result as RegExMatch
		list.append({
			"arg_name": result.get_string("ArgName"),
			"arg_type": result.get_string("ArgType").trim_prefix("="),
			"default_value": result.get_string("Value"),
		})
	return list
